﻿using DevExpress.XtraEditors;
using DevExpress.XtraEditors.DXErrorProvider;
using MM.Common.Base;
using MM.Common.DBBase;
using MM.Common.UI.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MM.Common.Support
{
    public partial class FrmGeneralDicItemSetupCenter : XtraForm
    {
        public FrmGeneralDicItemSetupCenter()
        {
            InitializeComponent();
        }

        public FrmGeneralDicItemSetupCenter(string dicttype, string dictname, int dict_item_id)
        {
            this.InitializeComponent();
            this.DT_TYPE.FieldValue = dicttype;
            this.DT_TYPE.Text = dicttype;
            this.txtTable.Text = dictname;
            this.Text = dictname;
            if (dict_item_id >= 0)
            {
                this.DT_CODE.Enabled = false;
                this.DT_ID.Text = dict_item_id.ToString();
                string commandtext = "SELECT * FROM MXHLY_SYS_DICTIONARY WHERE DT_ID = :dictid";
                DataParamCol parm = new DataParamCol(new DataParam[] { new InputDataParam("dictid", dict_item_id) });
                DataTable dt = new DataTable();
                if (SystemInfo.DbData.Retrieve(commandtext, parm, ref dt) && (dt.Rows.Count == 1))
                {
                    this.DT_NAME.FieldValue = dt.Rows[0]["DT_NAME"].ToString();
                    this.DT_CODE.FieldValue = dt.Rows[0]["DT_CODE"].ToString();
                    this.DT_PARENT_ID.FieldValue = dt.Rows[0]["DT_PARENT_ID"].ToString();
                    this.DT_IS_LEAF.FieldValue = dt.Rows[0]["DT_IS_LEAF"].ToString();
                    this.DT_SORT.FieldValue = dt.Rows[0]["DT_SORT"].ToString();
                    this.INPUTCODE1.FieldValue = dt.Rows[0]["INPUTCODE1"].ToString();
                    this.INPUTCODE2.FieldValue = dt.Rows[0]["INPUTCODE2"].ToString();
                    this.DT_ZCODE.FieldValue = dt.Rows[0]["DT_ZCODE"].ToString();
                    this.DT_USE.FieldValue = dt.Rows[0]["DT_USE"].ToString();
                    this.DT_DEFAULT_IS.FieldValue = dt.Rows[0]["DT_DEFAULT_IS"].ToString();
                }
                this.DT_NAME.Focus();
            }
            else
            {
                this.DT_CODE.Enabled = true;
                int data = 0;
                SystemInfo.DbData.ExecuteScalar<int>("SELECT MAX(DT_ID) FROM MXHLY_SYS_DICTIONARY", ref data);
                this.DT_ID.Text = (data + 1).ToString();
            }
            this.DT_NAME.LostFocus += new EventHandler(this.DT_NAME_LostFocus);
            this.DT_CODE.Focus();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            base.DialogResult = DialogResult.Cancel;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (this.dxValidationProvider1.Validate())
            {
                DataParamCol parms = new DataParamCol();
                string commandtext = "";
                string str2 = "1";
                if (GeneralDictionary.IsDictExist(this.DT_ID.Text.Trim()))
                {
                    str2 = "1";
                }
                else
                {
                    str2 = "0";
                }
                if (str2 == "1")
                {
                    commandtext = "UPDATE MXHLY_SYS_DICTIONARY SET ";
                    string str3 = "";
                    foreach (Control control in base.Controls)
                    {
                        if (control is IControlField)
                        {
                            IControlField field = control as IControlField;
                            if (field.FieldDBName != "")
                            {
                                string str6 = str3;
                                str3 = str6 + field.FieldDBName + " = :" + field.FieldDBName + ", ";
                                object fieldValue = field.FieldValue;
                                if ((field.FieldValue != null) && (field.FieldDBName == "DT_PARENT_ID"))
                                {
                                    fieldValue = SystemInfo.DbData.GetValue("MXHLY_SYS_DICTIONARY", "DT_CODE", field.FieldValue.ToString().Trim(), "DT_ID");
                                }
                                parms.Add(new InputDataParam(field.FieldDBName.Trim(), fieldValue));
                            }
                        }
                    }
                    commandtext = commandtext + str3.WipeLastSeparator() + " WHERE DT_ID = :id ";
                    parms.Add(new InputDataParam("id", this.DT_ID.Text.Trim()));
                }
                else
                {
                    int data = 0;
                    SystemInfo.DbData.ExecuteScalar<int>("SELECT MAX(DT_ID) FROM MXHLY_SYS_DICTIONARY", ref data);
                    this.DT_ID.Text = (data + 1).ToString();
                    commandtext = "INSERT INTO MXHLY_SYS_DICTIONARY (";
                    string str4 = "";
                    string str5 = "";
                    foreach (Control control2 in base.Controls)
                    {
                        if (control2 is IControlField)
                        {
                            IControlField field2 = control2 as IControlField;
                            if (field2.FieldDBName != "")
                            {
                                str4 = str4 + field2.FieldDBName + ", ";
                                str5 = str5 + ":" + field2.FieldDBName + ", ";
                                object paramValue = field2.FieldValue;
                                if ((field2.FieldValue != null) && (field2.FieldDBName == "DT_PARENT_ID"))
                                {
                                    paramValue = SystemInfo.DbData.GetValue("MXHLY_SYS_DICTIONARY", "DT_CODE", field2.FieldValue.ToString().Trim(), "DT_ID");
                                }
                                parms.Add(new InputDataParam(field2.FieldDBName.Trim(), paramValue));
                            }
                        }
                    }
                    string str7 = commandtext;
                    commandtext = str7 + str4.WipeLastSeparator() + ") VALUES (" + str5.WipeLastSeparator() + ")";
                }
                if (SystemInfo.DbData.ExecuteSql(commandtext, parms) == 1)
                {
                    if (str2 == "1")
                    {
                        StringBuilder builder = new StringBuilder();
                        builder.Append("UPDATE KTSYS_DICTIONARY_LOG SET ");
                        builder.Append(" APPLICANT_DATE = sydate,ORG_CODE = :orgCode,CREATE_EMP_ID = :createEmpId,DIC_FLAG ='0'");
                        builder.Append(" WHERE DT_ID = :dtId");
                        DataParamCol col2 = new DataParamCol(new DataParam[] { new InputDataParam("orgCode", this.scmbManageOrg.FieldValue), new InputDataParam("createEmpId", SystemInfo.OperId), new InputDataParam("dtId", this.DT_ID.Text) });
                        SystemInfo.DbData.ExecuteSql(builder.ToString(), col2);
                    }
                    else
                    {
                        StringBuilder builder2 = new StringBuilder();
                        builder2.Append("INSERT INTO KTSYS_DICTIONARY_LOG(");
                        builder2.Append("LOG_ID,DT_ID,APPLICANT_DATE,ORG_CODE,CREATE_EMP_ID,DIC_FLAG)");
                        builder2.Append(" VALUES (");
                        builder2.Append(":logId,:dtId,sysdate,:orgCode,:createEmpId,'0')");
                        DataParamCol col3 = new DataParamCol(new DataParam[] { new InputDataParam("logId", GetLogId()), new InputDataParam("dtId", this.DT_ID.Text), new InputDataParam("orgCode", this.scmbManageOrg.FieldValue), new InputDataParam("createEmpId", SystemInfo.OperId) });
                        SystemInfo.DbData.ExecuteSql(builder2.ToString(), col3);
                    }
                    base.DialogResult = DialogResult.OK;
                    base.Close();
                }
                else
                {
                    XtraMessageBox.Show("保存数据出错！\r\n" + SystemInfo.DbData.ErrorMessage);
                }
            }
        }

        private void DT_NAME_Leave(object sender, EventArgs e)
        {
            this.INPUTCODE1.EditValue = IME.GetInputCode(this.DT_NAME.EditValue.ToString(), "1");
            this.INPUTCODE2.EditValue = IME.GetInputCode(this.DT_NAME.EditValue.ToString(), "2");
        }

        private void DT_NAME_LostFocus(object sender, EventArgs e)
        {
            if ((this.DT_NAME.EditValue != null) && !string.IsNullOrEmpty(this.DT_NAME.EditValue.ToString()))
            {
                this.INPUTCODE1.EditValue = IME.GetInputCode(this.DT_NAME.EditValue.ToString(), "1");
                this.INPUTCODE2.EditValue = IME.GetInputCode(this.DT_NAME.EditValue.ToString(), "2");
            }
        }

        private void FrmGeneralDictionarySetup_Load(object sender, EventArgs e)
        {
            ConditionValidationRule rule = new ConditionValidationRule
            {
                ConditionOperator = ConditionOperator.IsNotBlank,
                ErrorText = "该值不能为空！"
            };
            this.dxValidationProvider1.SetValidationRule(this.DT_ID, rule);
            this.dxValidationProvider1.SetValidationRule(this.DT_CODE, rule);
            this.dxValidationProvider1.SetValidationRule(this.DT_NAME, rule);
            this.dxValidationProvider1.SetValidationRule(this.txtTable, rule);
        }

        private static decimal GetLogId()
        {
            decimal num = 0M;
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT (ISNULL(MAX(LOG_ID),0)+1)  LOG_ID FROM  KTSYS_DICTIONARY_LOG");
            DataTable dt = new DataTable();
            SystemInfo.DbData.Retrieve(builder.ToString(), ref dt);
            if ((dt.Rows.Count > 0) && (dt.Rows[0]["LOG_ID"] != null))
            {
                num = decimal.Parse(dt.Rows[0]["LOG_ID"].ToString());
            }
            return num;
        }
        // Properties
        public int DictionaryID
        {
            get
            {
                return int.Parse(this.DT_ID.Text);
            }
        }
    }
}
